const routerFile = require("../../utils/router.js");
const router = routerFile.router;

const requestFile = require("../../utils/request.js");
const request = requestFile.request;

const noticeFile = require("../../utils/notice.js");
const Toast = noticeFile.Toast;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: "sh1621946218990wz",
    status: "",
    applyAccount: "",
    applyUser: "",
    applyDept: "",
    applyDate: "",
    applyItems: [],
    useDate: "",
    returnDate: "",

    pickUser: [],
    descriptions: [],
  },

  onLoad: function (options) {
    var that = this;
    const userModal = wx.getStorageSync('user');
    const orderId = options.orderId;
    if (userModal == null || userModal == undefined || userModal == "") {
      wx.navigateTo({
        url: 'pages/login/login',
      })
    }
    Toast.loading("订单加载中");
    request(router.getOrder, "GET", {
      all: false,
      userModal: userModal,
      orderId: orderId,
    }).then(function(res) {
      const order = res.data.data.order;
      that.setData({
        orderId: order.orderId,
        status: order.status,
        applyAccount: order.applyUser.account,
        applyUser: order.applyUser.description,
        applyDept: order.dept.name,
        applyDate: that.formatDateString(order.applyDate),
        applyItems: order.items,
        useDate: that.formatDateString(order.useDate),
        returnDate: that.formatDateString(returnDate)
      });
      Toast.success("加载成功");
    }).catch(function(errMsg) {
      Toast.fail("订单获取失败")
    })
  },

  
  formatDateString: function(dateStr="") {
    var datePattern = /[\d]{4}-[\d]{2}-[\d]{2}/;
    var timePattern = /[\d]{2}:[\d]{2}/;
    var date = dateStr.match(datePattern)[0];
    var time = dateStr.match(timePattern)[0];
    var times = time.split(":");
    var hour = times[0];
    var calHour = parseInt(hour) + 8;
    return date + " " + calHour + ":" + times[1];
  },

  onShow: function() {
    var that = this;
    request(router.getManageUser, "GET", {}).then(function(res) {
      const users = res.data.data.users;
      var descs = new Array();
      for (var i = 0; i < users.length; i++) {
        const user = users[i];
        descs[i] = user.account + "-" + user.description + "-" + user.auth;
      }
      that.setData({
        pickUser: users,
        descriptions: descs,
      })
    })
  },

  doReject: function(e) {
    Toast.loading("订单处理中")
    const orderId = this.data.orderId;
    const userModal = wx.getStorageSync('user');
    request(router.checkOrder, "GET", {
      orderId: orderId,
      passed: false,
      userModal: userModal,
    }).then(function(res) {
      Toast.success("订单处理成功");
      wx.navigateTo({
        url: 'pages/index/index',
      })
    }).catch(function(errMsg) {
      Toast.fail("订单处理失败");
    })
  },
  
  chooseUser: function(e) {
    const chooseIndex = parseInt(e.detail.value);
    const orderId = this.data.orderId;
    const toUserId = this.data.pickUser[chooseIndex].id;
    const userModal = wx.getStorageSync('user');
    request(router.checkOrder, "GET", {
      orderId: orderId,
      passed: true,
      toUserId: toUserId,
      userModal: userModal,
    }).then(function(res) {
      Toast.success("订单处理成功");
      wx.navigateTo({
        url: 'pages/index/index',
      })
    }).catch(function(errMsg) {
      Toast.fail("订单处理失败！")
    })
  }
})